package com.cuiweiyou.middleman;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import java.util.concurrent.Executor;

/*
 * 中间人base不是系统加载的，没有天生的生命周期。
 * 实现IPluginActivityLifecycle，强加生命，赋予上下文。
 *
 * */
public class BasePluginActivity extends AppCompatActivity implements IPluginActivity {
    
    /* 通过中间人传入的应用(宿主)的上下文 */
    protected AppCompatActivity that;
    
    @Override
    public void attach(AppCompatActivity rootActivity) {
        that = rootActivity;
        Log.e("ard", "中间人base：获得上下文：" + that.getClass().getSimpleName());
    }
    
    @Override
    public void setContentView(View view) {
        if (null == that) {
            super.setContentView(view);
        } else {
            that.setContentView(view);
        }
        Log.e("ard", "中间人base：执行：setContentView(View view)");
    }
    
    @Override
    public void setContentView(int layoutResID) {
        that.setContentView(layoutResID);
        Log.e("ard", "中间人base：执行：setContentView(int layoutResID)");
    }
    
    @Override
    public <T extends View> T findViewById(int id) {
        Log.e("ard", "中间人base：执行：View findViewById(int id)");
        return that.findViewById(id);
    }
    
    @Override
    public Intent getIntent() {
        Log.e("ard", "中间人base：执行：Intent getIntent()");
        return that.getIntent();
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.e("ard", "中间人base：执行：onCreate(Bundle savedInstanceState)");
    }
    
    @Override
    public void onStart() {
        Log.e("ard", "中间人base：执行：onStart()");
    }
    
    @Override
    public void onRestart() {
        Log.e("ard", "中间人base：执行：onRestart()");
    }
    
    @Override
    public void onNewIntent(Intent intent) {
        Log.e("ard", "中间人base：执行：onNewIntent(intent)");
    }
    
    @Override
    public void onResume() {
        Log.e("ard", "中间人base：执行：onResume()");
    }
    
    @Override
    public void onPause() {
        Log.e("ard", "中间人base：执行：onPause()");
    }
    
    @Override
    public void onStop() {
        Log.e("ard", "中间人base：执行：onStop()");
    }
    
    @Override
    public void onDestroy() {
        Log.e("ard", "中间人base：执行：onDestroy()");
    }
    
    @Override
    public void onSaveInstanceState(Bundle outState) {
        Log.e("ard", "中间人base：执行：onSaveInstanceState(outState)");
    }
    
    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        Log.e("ard", "中间人base：执行：onSaveInstanceState(outState, outPersistentState)");
    }
    
    @Override
    public void onTouchEvent() {
        Log.e("ard", "中间人base：执行：onTouchEvent()");
    }
    
    @Override
    public LayoutInflater getLayoutInflater() {
        Log.e("ard", "中间人base：执行：LayoutInflater getLayoutInflater()");
        return that.getWindow().getLayoutInflater();
    }
    
    @Override
    public ClassLoader getClassLoader() {
        Log.e("ard", "中间人base：执行：ClassLoader getClassLoader()");
        return PluginManager.getInstance().getDexClassLoader();
    }
    
    public Context getBaseContext() {
        Log.e("ard", "中间人base：执行：Context getBaseContext()");
        return that.getBaseContext();
    }
    
    @Override
    public AssetManager getAssets() {
        Log.e("ard", "中间人base：执行：AssetManager getAssets()");
        return that.getAssets();
    }
    
    @Override
    public Resources getResources() {
        Log.e("ard", "中间人base：执行：Resources getResources()");
        return PluginManager.getInstance().getResources();
    }
    
    @Override
    public PackageManager getPackageManager() {
        Log.e("ard", "中间人base：执行：PackageManager getPackageManager()");
        return that.getPackageManager();
    }
    
    @Override
    public ContentResolver getContentResolver() {
        Log.e("ard", "中间人base：执行：ContentResolver getContentResolver()");
        return that.getContentResolver();
    }
    
    @Override
    public Looper getMainLooper() {
        Log.e("ard", "中间人base：执行：Looper getMainLooper()");
        return that.getMainLooper();
    }
    
    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    public Executor getMainExecutor() {
        Log.e("ard", "中间人base：执行：Executor getMainExecutor()");
        return that.getMainExecutor();
    }
    
    @Override
    public Context getApplicationContext() {
        Log.e("ard", "中间人base：执行：Context getApplicationContext()");
        return that.getApplicationContext();
    }
    
    @Override
    public void startActivity(Intent intent) {
        Intent i = new Intent();
        i.putExtra("className", intent.getComponent().getClassName());
        that.startActivity(i);
        Log.e("ard", "中间人base：执行：startActivity(Intent intent)");
    }
    
    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        Intent i = new Intent();
        i.putExtra("className", intent.getComponent().getClassName());
        that.startActivityForResult(i, requestCode);
        Log.e("ard", "中间人base：执行：startActivityForResult(intent, requestCode)");
    }
    
    @Override
    public WindowManager getWindowManager() {
        Log.e("ard", "中间人base：执行：WindowManager getWindowManager()");
        return that.getWindowManager();
    }
    
    @Override
    public ApplicationInfo getApplicationInfo() {
        Log.e("ard", "中间人base：执行：ApplicationInfo getApplicationInfo()");
        if (null == that){
            return super.getApplicationInfo();
        }else {
            return that.getApplicationInfo();
        }
    }
    
    @Override
    public Window getWindow() {
        Log.e("ard", "中间人base：执行：Window getWindow()");
        return that.getWindow();
    }
}


